package com.fenghuajueli.module_user.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.lib_data.constants.EventBusConstants;
import com.fenghuajueli.lib_data.entity.eventbus.EventEntity;
import com.fenghuajueli.module_user.R;
import com.fenghuajueli.module_user.constants.PayConfig;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

public class WXPaymentActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wxpay_result_layout);

        api = WXAPIFactory.createWXAPI(this, PayConfig.WECHAT_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp baseResp) {
        Log.d(TAG, "onPayFinish, errCode = " + baseResp.errCode);
        switch (baseResp.errCode) {
            case 0:
                //成功
                ToastUtils.showShort( "支付成功");
                EventBus.getDefault().post(new EventEntity(EventBusConstants.PAY_SUCCESS, null));
                break;
            case -1:
                //失败
                ToastUtils.showShort( "支付失败");
                EventBus.getDefault().post(new EventEntity(EventBusConstants.PAY_ERROR, null));
                break;
            case -2:
                //用户取消
                ToastUtils.showShort( "支付取消");
                EventBus.getDefault().post(new EventEntity(EventBusConstants.PAY_ERROR, null));
                break;
        }
        finish();
    }
}